//
//  ByViewDetailShare.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/15.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewDetailShare.h"

@interface ByViewDetailShare ()

@property (nonatomic, strong) UIView *bgView;

@end

@implementation ByViewDetailShare
{
    NSArray     *_titles;
    NSArray     *_images;
    NSInteger   _numOfLines;
}
- (instancetype)initWithTitles:(NSArray *)titles
                        images:(NSArray *)images
                    numOfLines:(NSInteger)numOfLines {
    
    if (self = [super init]) {
        
        _titles = titles;
        _images = images;
        _numOfLines = numOfLines;
        [self setupViews];
        
        self.contentView = self.bgView;
    }
    return self;
}

- (UIView *)bgView {
    
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (void)setupViews {
    
    NSInteger count = (NSInteger)MAX(_titles.count, _images.count);
    CGFloat x, y, width, height;
    
    for (int i = 0; i < count; i ++) {
        width = self.bgView.width / _numOfLines;
        height = width;
        x = (i % _numOfLines) * width;
        y = (i / _numOfLines) * height;
        // 1.创建基底视图
        UIControl *basalView = [[UIControl alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [self.bgView addSubview:basalView];
        
        // 2.创建图片
        x = 0;
        y = 0;
        width = basalView.height * 0.4;
        height = width;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        imageView.image = [UIImage imageNamed:_images[i]];
        [basalView addSubview:imageView];
        
        // 3.创建文字
        x = 0;
        y = 0;
        width = basalView.width;
        height = 20;
        CGFloat fontSize = 14;
        CGFloat width = basalView.width;
        CGFloat height = [_titles[i] getSizeOfString:fontSize].height;
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        textLabel.adjustsFontSizeToFitWidth = YES;
        textLabel.font = FONT(fontSize);
        textLabel.text = _titles[i];
        textLabel.textColor = TEXT_COLOR;
        textLabel.textAlignment = NSTextAlignmentCenter;
        [basalView addSubview:textLabel];
        
        // 4.适配距离
        if (_titles && _images) {
            CGFloat remine = basalView.height - imageView.height - textLabel.height - 5;
            imageView.top = remine / 2;
            imageView.x = basalView.width / 2;
            textLabel.top = imageView.bottom + 5;
            textLabel.x = basalView.width / 2;
        } else {
            if (_titles) {
                imageView.hidden = YES;
                textLabel.font = FONT(16);
                textLabel.x = basalView.width / 2;
                textLabel.y = basalView.height / 2;
            } else {
                textLabel.hidden = YES;
                imageView.x = basalView.width / 2;
                imageView.y = basalView.height / 2;
            }
        }
        
    }
    
    self.bgView.height = self.bgView.subviews.lastObject.bottom;
}
@end
